const getImageExts = (type:'string'|'array'='string'):any=>{
    if(type == 'string'){
        return '.jpg,.jpeg,.png,.gif,.bmp';
    }
    return ['jpg','jpeg','png','gif','bmp'];
}
const getDocExts = (type:'string'|'array'='string'):any=>{
    if(type == 'string'){
        return '.doc,.docx,.pdf';
    }
    return ['doc','docx','pdf'];
}
const getTxtExts = (type:'string'|'array'='string'):any=>{
    if(type == 'string'){
        return '.txt';
    }
    return ['txt'];
}

export const useRules = {
    // 需小于当前时间
    lessThanNow:(tips:string)=>{
        return {
            validator: (value:any, cb:any) => {
                return new Promise(resolve => {
                    if(Number(value)>Date.now()/1000){
                        cb(tips)
                    }
                    resolve('')
                })
            }
        }
    },
    // 需大于当前时间
    moreThanNow:(tips:string)=>{
        return {
            validator: (value:any, cb:any) => {
                return new Promise(resolve => {
                    if(Number(value)<Date.now()/1000){
                        cb(tips)
                    }
                    resolve('')
                })
            }
        }
    },
    // 需大于0
    moreThanZero:(tips:string)=>{
        return {
            validator: (value:any, cb:any) => {
                return new Promise(resolve => {
                    if(Number(value)<=0){
                        cb(tips)
                    }
                    resolve('')
                })
            }
        }
    },
    rangePickerRequired:(tips:string)=>{
        return {
            validator: (value:any, cb:any) => {
                return new Promise(resolve => {
                    if(!value?.start || !value?.end){
                        cb(tips)
                    }
                    resolve('')
                })
            }
        }
    },
    selectRequired:(tips:string)=>{
        return {
            validator: (value:any, cb:any) => {
                return new Promise(resolve => {
                    if((typeof value == 'string' && !value) || (typeof value == 'object' && !value?.length) || (typeof value == 'number' && value === 0)) {
                        cb(tips)
                    }
                    resolve('')
                })
            }
        }
    },
    check:{
        getImageExts,
        getDocExts,
        getTxtExts,
        isImage:(fileName:string)=>{
            const ext = fileName?.split('.')?.pop()?.toLowerCase() || '';
            return getImageExts('array').includes(ext);
        },
        isDoc:(fileName:string)=>{
            const ext = fileName?.split('.')?.pop()?.toLowerCase() || '';
            return getDocExts('array').includes(ext);
        },
        isTxt:(fileName:string)=>{
            const ext = fileName?.split('.')?.pop()?.toLowerCase() || '';
            return getTxtExts('array').includes(ext);
        }
    }
}